{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Collocating external data\n\nScript will use py-eddy-tracker methods to upload external data (sea surface temperature, SST)\nin a common structure with altimetry.\n\nFigures higlights the different steps.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from datetime import datetime\n\nfrom matplotlib import pyplot as plt\n\nfrom py_eddy_tracker import data\nfrom py_eddy_tracker.dataset.grid import RegularGridDataset\n\ndate = datetime(2016, 7, 7)\n\nfilename_alt = data.get_demo_path(\n    f\"dt_blacksea_allsat_phy_l4_{date:%Y%m%d}_20200801.nc\"\n)\nfilename_sst = data.get_demo_path(\n    f\"{date:%Y%m%d}000000-GOS-L4_GHRSST-SSTfnd-OISST_HR_REP-BLK-v02.0-fv01.0.nc\"\n)\nvar_name_sst = \"analysed_sst\"\n\nextent = [27, 42, 40.5, 47]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Loading data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sst = RegularGridDataset(filename=filename_sst, x_name=\"lon\", y_name=\"lat\")\nalti = RegularGridDataset(\n    data.get_demo_path(filename_alt), x_name=\"longitude\", y_name=\"latitude\"\n)\n# We can use `Grid` tools to interpolate ADT on the sst grid\nsst.regrid(alti, \"sla\")\nsst.add_uv(\"sla\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Functions to initiate figure axes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def start_axes(title, extent=extent):\n    fig = plt.figure(figsize=(13, 6), dpi=120)\n    ax = fig.add_axes([0.03, 0.05, 0.89, 0.91])\n    ax.set_xlim(extent[0], extent[1])\n    ax.set_ylim(extent[2], extent[3])\n    ax.set_title(title)\n    ax.set_aspect(\"equal\")\n    return ax\n\n\ndef update_axes(ax, mappable=None, unit=\"\"):\n    ax.grid()\n    if mappable:\n        cax = ax.figure.add_axes([0.93, 0.05, 0.01, 0.9], title=unit)\n        plt.colorbar(mappable, cax=cax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## ADT first display\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = start_axes(\"SLA\", extent=extent)\nm = sst.display(ax, \"sla\", vmin=0.05, vmax=0.35)\nupdate_axes(ax, m, unit=\"[m]\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## SST first display\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now plot SST from `sst`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = start_axes(\"SST\")\nm = sst.display(ax, \"analysed_sst\", vmin=295, vmax=300)\nupdate_axes(ax, m, unit=\"[\u00b0K]\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = start_axes(\"SST\")\nm = sst.display(ax, \"analysed_sst\", vmin=295, vmax=300)\nu, v = sst.grid(\"u\").T, sst.grid(\"v\").T\nax.quiver(sst.x_c[::3], sst.y_c[::3], u[::3, ::3], v[::3, ::3], scale=10)\nupdate_axes(ax, m, unit=\"[\u00b0K]\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, with eddy contours, and displaying SST anomaly\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sst.bessel_high_filter(\"analysed_sst\", 400)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Eddy detection\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sst.bessel_high_filter(\"sla\", 400)\n# ADT filtered\nax = start_axes(\"SLA\", extent=extent)\nm = sst.display(ax, \"sla\", vmin=-0.1, vmax=0.1)\nupdate_axes(ax, m, unit=\"[m]\")\na, c = sst.eddy_identification(\"sla\", \"u\", \"v\", date, 0.002)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "kwargs_a = dict(lw=2, label=\"Anticyclonic\", ref=-10, color=\"b\")\nkwargs_c = dict(lw=2, label=\"Cyclonic\", ref=-10, color=\"r\")\nax = start_axes(\"SST anomaly\")\nm = sst.display(ax, \"analysed_sst\", vmin=-1, vmax=1)\na.display(ax, **kwargs_a), c.display(ax, **kwargs_c)\nax.legend()\nupdate_axes(ax, m, unit=\"[\u00b0K]\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Example of post-processing\nGet mean of sst anomaly_high in each internal contour\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "anom_a = a.interp_grid(sst, \"analysed_sst\", method=\"mean\", intern=True)\nanom_c = c.interp_grid(sst, \"analysed_sst\", method=\"mean\", intern=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Are cyclonic (resp. anticyclonic) eddies generally associated with positive (resp. negative) SST anomaly ?\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=(7, 5))\nax = fig.add_axes([0.05, 0.05, 0.90, 0.90])\nax.set_xlabel(\"SST anomaly\")\nax.set_xlim([-1, 1])\nax.set_title(\"Histograms of SST anomalies\")\nax.hist(\n    anom_a, 5, alpha=0.5, color=\"b\", label=\"Anticyclonic (mean:%s)\" % (anom_a.mean())\n)\nax.hist(anom_c, 5, alpha=0.5, color=\"r\", label=\"Cyclonic (mean:%s)\" % (anom_c.mean()))\nax.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Not clearly so in that case ..\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}